package com.ly.common.business.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.codehaus.plexus.util.ExceptionUtils;
import org.codehaus.plexus.util.StringUtils;

import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;

/**
 * @author luoyana
 * @version V1.0
 * @ClassName BuinessUtil
 * @description 业务工具类
 * @update 2023/3/9
 */
@Slf4j
public class BusiUtil {

    public static boolean isNotNull(Object t) {
        return !isNull(t);
    }

    public static boolean isNull(Object t) {
        if(t == null){
            return true;
        }
        if(String.class.isInstance(t)){
            return StringUtils.isEmpty((String) t);
        }
        if(List.class.isInstance(t)){
            return ((List<?>) t).isEmpty();
        }
        if(Map.class.isInstance(t)){
            return ((Map<?, ?>) t).size()==0;
        }
        if(Set.class.isInstance(t)){
            return ((Set<?>) t).isEmpty();
        }
        return t == null;
    }

    public static boolean isEquals(Long s, Long t) {
        return s.equals(t);
    }

    public static boolean isEquals(String s, String t) {
        if (s == null && t == null) {
            return true;
        }
        if (s == null || t == null) {
            return false;
        }
        return s.equals(t);
    }

    public static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");

    public static boolean isInteger(String s) {
        if (s == null || s.length() == 0) {
            return false;
        }
        return INT_PATTERN.matcher(s).matches();
    }

    public static boolean isContains(String[] values, String value) {
        if (value != null && value.length() > 0 && values != null && values.length > 0) {
            for(String v:values){
                if(value.equals(v)){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isIn(Object source,Object...obj){
        boolean result = false;
        if(null != source && obj.length>0){
            for(int i = 0;i<obj.length;i++){
                if(source.toString().equals(obj[i])){
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public static String convertDate2Str(Date date){
        return convertDate2Str(date,null);
    }

    public static String convertDate2Str(Date date,String pattern){
        if(null == date){
            return null;
        }
        if(BusiUtil.isNull(pattern)){
            pattern = "yyyyMMdd";
        }
        return date == null ? null : DateFormatUtils.format(date,pattern);
    }

    public static Date convertStr2Date(String date){
        try {
            return convertStr2Date(date,"yyyyMMdd");
        }catch (Exception e){
            try {
                throw e;
            } catch (ParseException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public static Date convertStr2Date(String strDate,String...patterns) throws ParseException {
        Date date = null;
        if(StringUtils.isEmpty(strDate)){
            return null;
        }

        try {
            date = DateUtils.parseDate(strDate,patterns);
        }catch (ParseException e){
            log.error(ExceptionUtils.getStackTrace(e));
            throw e;
        }
        return date;
    }

    public static String getUUID(){
        ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
        String uuid = (new UUID((long) threadLocalRandom.nextInt(),(long)threadLocalRandom.nextInt())).toString();
        return uuid.replaceAll("-","");
    }
}
